package com.cn.icss.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.icss.service.IStreamCallBackService;
import com.cn.icss.service.impl.StreamCallBackServiceImpl;
import com.cn.icss.vo.CallBackRequestData;
import com.cn.icss.vo.common.ResponseCode;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import org.apache.log4j.Logger;

/**
 * @中软华腾软件系统有限公司,未经授权不得擅自传播
 * @类描述:
 * @类名称:StreamCallBackController
 * @作者: Administrator
 * @编辑时间: 2020/04/16 10:42
 * @版本: 1.0
 */
public class StreamCallBackController extends Controller {
    // Controller是线程安全的，说明是多利的
    private Logger logger = Logger.getLogger(StreamCallBackController.class);
    @Inject(value = StreamCallBackServiceImpl.class)
    private IStreamCallBackService streamCallBackService;

    // 连接上的回调
    @Before(POST.class)
    public void on_connect() {
        // 目前如下几种情形都会调用到这里来 1. 推流 2.播流
        // 这个地方做验证判断是否是登录系统的人才能进行后续的推流与播流 TODO 并且如果
        String json = super.getRawData();
        // 注意是接收传来的JSON数据
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
//        if (callBackRequestData != null) {
        // 过滤斜杠
       // this.filterStreamId(callBackRequestData);
        logger.info("连接过来了,调用了on_connect:" + callBackRequestData);
            // 判断tcurl中的参数rtmp://192.168.117.20:1935/2001/1001?tokenSecret=3d42315f98fc00760fa554567b0c1eae&token=6666&streamId=123456/123456
            // 如果流放前面拿不到后面参数
//            if(!streamCallBackService.authticateUserStream(callBackRequestData)){
//                renderText(ResponseCode.Error.getStatusCode().toString());
//                return ;
//            }
//        }
        renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
    }

    @Before(POST.class)
    public void on_publish() {
        String json = super.getRawData();
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
        if (callBackRequestData != null && streamCallBackService.authticateUserStream(callBackRequestData)) {
            logger.info("调用了on_publish:" + callBackRequestData);
            // 过滤斜杠
            //this.filterStreamId(callBackRequestData);
            // 如果以后要推流在这里判断是否该流已经有人在推
//            String streamId = callBackRequestData.getStream();
            // 修改状态增加人数
            streamCallBackService.changeONStatus(callBackRequestData);
            streamCallBackService.increaseOnlineNum(callBackRequestData);
            renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
        }else{
            renderText(ResponseCode.Error.getStatusCode().toString());
        }

    }

    @Before(POST.class)
    public void on_unpublish() {
        String json = super.getRawData();
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
        if (callBackRequestData != null) {
            logger.info("调用了on_unpublish:" + callBackRequestData);
            // 过滤斜杠
           // this.filterStreamId(callBackRequestData);
            // 推流结束就更改推流的状态和减少人数
            streamCallBackService.chageOFFStatus(callBackRequestData);
            streamCallBackService.decreaseOnlineNum(callBackRequestData);
        }
        renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
    }

    @Before(POST.class)
    public void on_play() {
        String json = super.getRawData();
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
        if (callBackRequestData != null && streamCallBackService.authticateUserStream(callBackRequestData)) {
            logger.info("调用了on_play:" + callBackRequestData);
            // 过滤斜杠
            //this.filterStreamId(callBackRequestData);
            // 播流成功以后就开始加1
            streamCallBackService.increaseOnlineNum(callBackRequestData);
            renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
        }else{
            renderText(ResponseCode.Error.getStatusCode().toString());
        }

    }

    @Before(POST.class)
    public void on_stop() {
        String json = super.getRawData();
        // 注意是接收传来的JSON数据
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
        if (callBackRequestData != null) {
            logger.info("调用了on_stop:" + callBackRequestData);
            // 过滤斜杠
            //this.filterStreamId(callBackRequestData);
            // 停止播放后就减人数
            streamCallBackService.decreaseOnlineNum(callBackRequestData);
        }
        renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
    }

    @Before(POST.class)
    public void on_close() {
        String json = super.getRawData();
        // 注意是接收传来的JSON数据
        CallBackRequestData callBackRequestData = JSONObject.parseObject(json, CallBackRequestData.class);
        if (callBackRequestData != null) {
            logger.info("调用了on_close:" + callBackRequestData);
        }
        renderText(ResponseCode.SUCCESSCODE.getStatusCode().toString());
    }

    // 过滤stream流中的斜杠
    private void filterStreamId(CallBackRequestData callBackRequestData){
        String streamId = callBackRequestData.getStream();
        if(streamId!=null){
            streamId = streamId.replaceAll(".*/", "");
            callBackRequestData.setStream(streamId);
        }

    }


}
